# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from pymongo import MongoClient

# ITEM_PIPELINES 需要在settings打开
class XpathPipeline:
    # 默认初始化时候
    def __init__(self):
        self.client =client = MongoClient('mongodb://localhost:27017/')
        db = client['test']
        self.users = db['users']
        # self.f =open('data.txt','w',encoding='utf-8')

    # 因为每次请求都会重新执行这个方法，所以把操作文件方法提取最前面
    def process_item(self, item, spider):
        # self.f.write(item['title']+item['price']+"\n")
        res = self.users.insert_one(dict(item))
        print(res.inserted_id)
        return item

    # 销毁时候
    def __del__(self):
        print('结束')
        # self.f.close()